varying vec4 diffuse,ambient;       //! material's diffuse & ambient
varying vec3 normal, viewerVec;     //! normal vector, viewer vector
varying vec4 shadowTexCoord;        //! coordinates of shadow map
//uniform int texWidth, texHeight;  //! size of texture
uniform sampler2D tex;              //! texture handler
//uniform sampler2D lpos;             //! handler for texture storing light directions
uniform vec3 lightVec;
uniform sampler2D depthMap;        //! shadow map handler
uniform mat4 lproj;
	
void main()
{
	vec3 n,halfV, R, vv, l;
	float NdotL,NdotHV;

	vec4 I, Ia, Id, Is;
	vec4 color = ambient;
	n = normalize(normal);
	vv = normalize( viewerVec );
	vec3 lightVec;
	vec2 coords;
	vec4 texel;
	
	I = vec4( 0.0, 0.0, 0.0, 1.0 );

	l = normalize( lightVec );
			
	NdotL = max(dot(n,l),0.0);
	
	R = 2.0 * NdotL * n - l;

	Id = diffuse * NdotL;
	Is = gl_FrontMaterial.specular * gl_LightSource[0].specular * pow( max( dot(R, vv), 0.0), gl_FrontMaterial.shininess );
	I += Id + Is;	


	//shadowTexCoord = normalize( shadowTexCoord );
	vec4 shadowV = texture2D( depthMap, shadowTexCoord.xy );
	//float shadow = texture2D( depthMap, shadowTexCoord.xy ).r;
	float lighted;
	
	if( shadowV.r < (shadowTexCoord.z/shadowTexCoord.w) )
		lighted = 0.5;
	else
		lighted = 1.0;

	gl_FragColor = I  * lighted;

}